package com.cheyun.netsalev3.view

import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.ActivityCustomerStoreBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.viewmodel.CustomerStoreActivityViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import java.io.Serializable

class CustomerStoreActivity : BaseActivity<ActivityCustomerStoreBinding>() {
    lateinit var adapter: MyAdapter
    override fun initContentView(): Int {
        return R.layout.activity_customer_store
    }

    private var id: Int = 0

    override fun initViewModel(): ViewModel? {
        id = intent.getIntExtra("id", 0)
        var factory = InjectorUtils.getCustomerStoreModelFactory(id)
        return ViewModelProviders.of(this, factory).get(CustomerStoreActivityViewModel::class.java)
    }


    override fun initData() {
        adapter = MyAdapter(R.layout.customer_store_activity_item, binding.viewModel)
        binding.recyclerView.adapter = adapter
        binding.viewModel?.data?.observe(this, Observer { list ->
            adapter.submitList(list)
        })

        var showEdit = false
        var showFollow = false
        var showShare = false

        var loginData = MySharedPreferences.getLoginData()
        loginData?.member?.apps?.receive?.apply {
            for (i in this) {
                if (i == 4) {
                    showEdit = true
                }
                if(i == 2){
                    showFollow = true
                }
                if(i == 5){
                    showShare = true
                }
            }
        }
        binding.editBut.visibility = if(showEdit) View.VISIBLE else View.GONE

        binding.viewModel?.daoDianDetailsParam?.observe(this, Observer { it ->
            binding.nameText.text = it.uname
            binding.leveText.text = it.customerlevel
            if (it.sex == 1) {
                FunctionUtils.myDrawableLeft(binding.nameText, R.drawable.a01_1_zbsh_headboy)
            } else {
                FunctionUtils.myDrawableLeft(binding.nameText, R.drawable.a01_1_zbsh_headgirl)
            }
            if(showFollow && !it.leavetime.isNullOrEmpty()){
                binding.btnFollow.visibility = View.VISIBLE
            }
        })
    }


    override fun initView() {
        binding.editBut.setOnClickListener {
            var intent = Intent(this, EditCustomStoreActivity::class.java)
            var param = binding.viewModel?.daoDianDetailsParam?.value
            param?.apply {
                intent.putExtra("data", this as Serializable)
            }
            startActivity(intent)
        }

        binding.btnDrive.setOnClickListener {

        }
        binding.btnFollow.setOnClickListener {

        }
        binding.dialBut.setOnClickListener {
            binding.viewModel?.daoDianDetailsParam?.value?.phone?.apply {
                FunctionUtils.callPhone(this@CustomerStoreActivity, this)
            }
        }

        LiveEventBus.get()
                .with("ReceptionActivity", String::class.java)
                .observe(this, Observer {
                    binding.viewModel?.loadData()
                })
    }
}
